import traceback
from django.core.management.base import BaseCommand
import logging
import re
import os
from apps.news.models import Record

class Command(BaseCommand):
	def handle(self, *args, **options):
		self.logger = logging.getLogger()
		print "Starting broken link finder"
		try:
			records = Record.objects.all()
			regex = re.compile("(http:\/\/xoxot.com.ua([A-Za-z0-9\.\_\-\=\+\/]+))",re.MULTILINE)
			
			r_list = []
			
			for record in records:
				links = regex.findall(record.story)
				
				for link in links:
					if not os.path.exists("/var/www/vhosts/live_engine%s" % link[1]):
						if not record.pk in r_list:
							r_list.append(record.pk)
							print "http://xoxot.com.ua/admin/news/record/%s/" % record.pk
							print link[1]
							
			print "Total: %s" % len(r_list)
					
		except:
			traceback.print_exc()
